package stu2;

import java.util.Random;
import java.util.Scanner;

public class StuTest {
    Student[] stus=new Student[10];
    Scanner input=new Scanner(System.in);

    Random rad=new Random();

    public static void main(String[] args) {
        StuTest st=new StuTest();
        st.start();
    }

    private void start() {
        initStudent();
        int choose;
        do {
            printInfo();
            choose=input.nextInt();
            switch (choose){
                case 1:
                    printStu();
                    break;
                case 2:
                    searchState();
                    break;
                case 3:
                    sortStu();
                    break;
                case 0:

                    break;

            }

        }while (choose!=0);
    }

    private void sortStu() {
        //冒泡排序
        for (int i=0;i<stus.length;i++){
            for (int j=0;j<stus.length-i-1;j++){
                if(stus[j].score>stus[j+1].score){
                    //交换
                    Student tempStu=stus[j];
                    stus[j]=stus[j+1];
                    stus[j+1]=tempStu;
                }
            }
        }


    }

    private void searchState() {
        System.out.println("请输入你要查询的学号");
        int num=input.nextInt();
        for (Student stu:stus){
            if(stu.sNo==num){
                stu.printInfo();
            }
        }
    }

    private void printStu() {
        for (Student st:stus){
            st.printInfo();
        }
    }

    private void initStudent() {
        for (int i = 0; i < stus.length; i++) {
            stus[i]=new Student();
            stus[i].sNo=i+1;
            stus[i].grade=rad.nextInt(6)+1;
            stus[i].score=rad.nextInt(100)+1;
        }
    }

    private void printInfo() {
        System.out.println("---学生管理系统---");
        System.out.println("1打印学生信息");
        System.out.println("2查找学生");
        System.out.println("3按成绩排序");
        System.out.println("0退出");
        System.out.println("请选择");
    }
}
